//
//  WCBInputBusinessCodeViewController.m
//  WeChatHelper
//
//  Created by Guo Rui on 2017/12/12.
//  Copyright © 2017年 TimeFace. All rights reserved.
//

#import "WCBInputBusinessCodeViewController.h"
#import "WCBDataHelper.h"
#import "WCBPhoneLoginViewController.h"
//#import "WeChatRobot.h"
#import "WCBFormatTextField.h"
#import "TFToast.h"
#import "WCASessionManager.h"
#import "WCBCaptureDataViewController.h"

@interface WCBInputBusinessCodeViewController ()
@property (strong, nonatomic) UIView *backgroundView;
@property (strong, nonatomic) UILabel *inputTitleLabel;
@property (strong, nonatomic) WCBFormatTextField *textField;
@property (strong, nonatomic) UILabel *inputTailLabel;
@property (strong, nonatomic) UIButton *skipButton;
@property (strong, nonatomic) UIButton *commitButton;
@property (strong, nonatomic) UILabel *tipLabel;
@end

@implementation WCBInputBusinessCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1.00];
    self.title = @"渠道码绑定";
    
    [self setupViewLayout];

}

#pragma mark - Private
- (void)setupViewLayout
{
    [self backgroundView];
    
    CGFloat x, y, width, height;
    CGFloat v_width = CGRectGetWidth(self.view.frame);
    CGFloat v_height = CGRectGetHeight(self.view.frame);
    
    CGFloat tf_width = 300;
    
    x = 80;
    y = 160.f;
    width = v_width - 2 * x;
    height = 40.f;
    self.inputTitleLabel.frame = CGRectMake(x, y, width, height);

    width = tf_width;
    x = 80;
    y += (25.f + height);
    height = 60.f;
    self.textField.frame = CGRectMake(x, y, width, height);
    
    x = 80;
    y += (25.f + height);
    width = v_width - 2 * x;
    height = 40.f;
    self.inputTailLabel.frame = CGRectMake(x, y, width, height);
    
    x += 15.f;
    width = 250.f;
    y += (45.f + height);
    height = 55.f;
    self.skipButton.frame = CGRectMake(x, y, width, height);
    
    width = 250.f;
    x = v_width - 80 - width - 15;
//    y += (65.f + height);
    height = 55.f;
    self.commitButton.frame = CGRectMake(x, y, width, height);

    x = 0;
    y = 0;
    width = v_width;
    height = CGRectGetMaxY(self.commitButton.frame) + 30.f;
    self.backgroundView.frame = CGRectMake(x, y, width, height);
    
    x = 30.f;
    width = v_width - 2 * x;
    height = 35.f;
    y = v_height - 20 - height;
    self.tipLabel.frame = CGRectMake(x, y, width, height);
}

- (void)alterWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)titles clickBlock:(void (^)(int index))block
{
    UIAlertController *alt = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alt animated:YES completion:nil];
    
    for (int i = 0; i < titles.count; i++) {
        NSString *bt = titles[i];
        UIAlertAction *action = [UIAlertAction actionWithTitle:bt style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (block) block(i);
        }];
        [alt addAction:action];
    }
}

#pragma mark - Actions
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textField resignFirstResponder];
}

- (void)skipDidClick
{
    NSLog(@"%s", __func__);
    [self alterWithTitle:@"提示" message:@"如您未填写商家渠道码，那么通过本软件生成的订单将无法为您提供收益，如需帮助请致电400-155-1399垂询" buttonTitles:@[@"取消", @"无需收益"] clickBlock:^(int index) {
        if (index == 1) {
            WCBDataHelper.sharedHelper.buniessCode = @"new";
            WCBPhoneLoginViewController *vc = [[WCBPhoneLoginViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        NSLog(@"Cancel");
    }];
}

- (void)commitDidClick
{
    NSString *code = [self.textField m_text];
    if (code == nil || code.length == 0) {
        [TFToast toast:@"请输入渠道码"];
        return;
    }
    
    if (code.length < 12) {
        [TFToast toast:@"您输入的渠道码位数不足"];
        return;
    }
    

    self.commitButton.enabled = NO;
    __weak WCBInputBusinessCodeViewController * _self = self;
    [WCASessionManager POST:@"weimport/mtime/checkDealerCode" parameters:@{ @"dealerCode" : code } success:^(NSURLSessionDataTask *task, id responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
            _self.commitButton.enabled = YES;
            if ([[responseObject objectForKey:@"code"] isEqualToString:@"000"]) {
                WCBDataHelper.sharedHelper.buniessCode = code;
                
                [_self alterWithTitle:@"验证成功" message:@"您的商家身份已经确认，系统将记录订单收益到您的账户中如需更换商家渠道码请重装软件或致电400-155-1399垂询" buttonTitles:@[@"确认"] clickBlock:^(int index) {
                    WCBPhoneLoginViewController *vc = [[WCBPhoneLoginViewController alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }];
            }
            else {
                [TFToast toast:@"您输入的渠道码有误，请检查后再输入"];
            }
        });
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [TFToast toast:error.localizedDescription];
        });
    }];
}

#pragma mark - Getter
- (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
        _backgroundView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_backgroundView];
    }
    return _backgroundView;
}

- (UILabel *)inputTitleLabel
{
    if (!_inputTitleLabel) {
        _inputTitleLabel = [[UILabel alloc] init];
        _inputTitleLabel.font = [UIFont boldSystemFontOfSize:25];
        _inputTitleLabel.textColor = [UIColor blackColor];
        _inputTitleLabel.text = @"请输入您的商家渠道码";
        [self.view addSubview:_inputTitleLabel];
    }
    return _inputTitleLabel;
}

- (WCBFormatTextField *)textField
{
    if (!_textField) {
        _textField = [[WCBFormatTextField alloc] init];
        _textField.textColor = [UIColor blackColor];
        _textField.font = [UIFont systemFontOfSize:34];
        _textField.placeholder = @"xxxx xxxx xxxx";
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.text = @"102110571241";
        [self.view addSubview:_textField];
    }
    return _textField;
}

- (UILabel *)inputTailLabel
{
    if (!_inputTailLabel) {
        _inputTailLabel = [[UILabel alloc] init];
        _inputTailLabel.textColor = [UIColor orangeColor];
        _inputTailLabel.font = [UIFont systemFontOfSize:20];
        _inputTailLabel.text = @"商家码是判断订单收益归属的唯一标识，请务必填写";
        [self.view addSubview:_inputTailLabel];
    }
    return _inputTailLabel;
}

- (UILabel *)tipLabel
{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.textColor = [UIColor lightGrayColor];
        _tipLabel.font = [UIFont systemFontOfSize:20];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.text = @"温馨提示: 若您不清楚如何填写，请致电 400-155-1399 垂询";
        [self.view addSubview:_tipLabel];
    }
    return _tipLabel;
}

- (UIButton *)skipButton
{
    if (!_skipButton) {
        _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _skipButton.backgroundColor = [UIColor clearColor];
        _skipButton.layer.cornerRadius = 3.f;
        _skipButton.layer.masksToBounds = YES;
        _skipButton.layer.borderColor = [UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00].CGColor;
        _skipButton.layer.borderWidth = 1.f;
        _skipButton.titleLabel.font = [UIFont systemFontOfSize:22];
        [_skipButton setTitle:@"直接跳过" forState:UIControlStateNormal];
        [_skipButton setTitleColor:[UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00] forState:UIControlStateNormal];
        [_skipButton addTarget:self action:@selector(skipDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_skipButton];
    }
    return _skipButton;
}

- (UIButton *)commitButton
{
    if (!_commitButton) {
        _commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitButton.backgroundColor = [UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00];
        _commitButton.layer.cornerRadius = 3.f;
        _commitButton.layer.masksToBounds = YES;
        _commitButton.titleLabel.font = [UIFont systemFontOfSize:22];
        [_commitButton setTitle:@"提交验证" forState:UIControlStateNormal];
        [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
        [_commitButton addTarget:self action:@selector(commitDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_commitButton];
    }
    return _commitButton;
}

@end
